package com.ww.disruptor.consumer;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.ww.disruptor.event.ProductEvent;
import com.ww.disruptor.factory.CustomizableThreadFactory;
import com.ww.disruptor.factory.ProductEventFactory;
import com.ww.disruptor.handler.MultiThanHandler;
import com.ww.disruptor.producer.ProductEventProducer;

import java.util.UUID;
import java.util.concurrent.ThreadFactory;

public class MultiThanHandlerDemo02 {

    public static void main(String[] args) {
        ThreadFactory threadFactory = new CustomizableThreadFactory("customer-pool-");
        Disruptor<ProductEvent> disruptor = new Disruptor<>(new ProductEventFactory(), 4, threadFactory, ProducerType.SINGLE, new YieldingWaitStrategy());

        // 组合模式集群模式
        // 可以看到two和three在one只有才执行，four只有two和three都执行完毕才执行。
        disruptor.handleEventsWith(new MultiThanHandler("one"))
                .then(new MultiThanHandler("two"), new MultiThanHandler("three"))
                .then(new MultiThanHandler("four"));

        // 启动
        disruptor.start();

        RingBuffer<ProductEvent> ringBuffer = disruptor.getRingBuffer();
        ProductEventProducer producer = new ProductEventProducer(ringBuffer);

        for (int i = 0; i < 3; i++) {
            producer.onData(i + ", " + UUID.randomUUID());
        }

        // 终止
        disruptor.shutdown();
    }
}
